#include <genrand.h>
// generate random init
static int genRandInit()
{
    static DEV_C51_XDATA_FLAG uint8_t initflag = 0;
    if (!initflag)
    {
        srand(systemNowMs() + 0xC27A);
    }
    initflag = 1;
    return 0;
}
// generate random char buffer
static int genRandBase(char *outstr, int outsize, const char *genbuff, int gensize)
{
    int i;
    // logdebug("need size=%d", outsize);
    if (outstr == NULL || outsize <= 0)
    {
        logerror("");
        return -1;
    }
    genRandInit();
    if (genbuff == NULL || gensize <= 0)
    {
        for (i = 0; i < outsize; i++)
        {
            outstr[i] = rand();
        }
        return outsize;
    }
    for (i = 0; i < outsize; i++)
    {
        outstr[i] = genbuff[rand() % gensize];
    }
    return outsize;
}
// generate rand string, can be read.
int grdStrNorm(char *outstr, int outsize)
{
    static DEV_C51_CODE_FLAG const char tmpbuffer[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    return genRandBase(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}
#if DEVICE_PLATFORM_CONFIGURE != DEVICE_PLATFORM_C51_MCU
// generate rand char Hex buffer
int grdHexBuff(char *outstr, int outsize)
{
    return genRandBase(outstr, outsize, NULL, 0);
}
// generate rand string only digit
int grdStrDigit(char *outstr, int outsize)
{
    static const char tmpbuffer[] = "0123456789";
    return genRandBase(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}
// generate rand hex string, can be read.
int grdStrHex(char *outstr, int outsize)
{
    static const char tmpbuffer[] = "0123456789ABCDEF";
    return genRandBase(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}
// generate rand string with special, can be read.
int grdStrSpec(char *outstr, int outsize)
{
    static const char tmpbuffer[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$^&*()_+{}:|<>?`;";
    return genRandBase(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}
#endif
